
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class MainMidlet extends MIDlet implements CommandListener,ItemCommandListener{

    private CricketGame cricketGame;
    private Form mMenu;
    private Form mSetting;
    private Form mHighScore;
    private Form mHelp;
    private Form mAbout;
    private StringItem mSNewGame;
    private StringItem mSSetting;
    private StringItem mSHighScore;
    private StringItem mSHelp;
    private StringItem mSAbout;
    private Command CMD_EXIT;
    private Command CMD_SELECT;
    private Command CMD_BACK;
    
    public MainMidlet()
    {
        cricketGame = new CricketGame();
        mMenu = new Form("Menu Game");
        mSetting = new Form("Setting");
        mHighScore = new Form("High Score");
        mHelp = new Form("Help");
        mAbout = new Form("About");
        mSNewGame = new StringItem("", "New Game", StringItem.BUTTON);
        mSSetting = new StringItem("", "    Setting    ", StringItem.BUTTON);
        mSHighScore = new StringItem("", "High Score", StringItem.BUTTON);
        mSHelp = new StringItem("", "     Help     ", StringItem.BUTTON);
        mSAbout = new StringItem("", "    About    ", StringItem.BUTTON);
        CMD_EXIT = new Command("Exit", Command.SCREEN, 0);
        CMD_SELECT = new Command("Select", Command.SCREEN, 0);
        CMD_BACK = new Command("Back", Command.SCREEN, 0);
    }
    
    public void createMainMenu()
    {
        mSNewGame.setDefaultCommand(CMD_SELECT);
        mSNewGame.setItemCommandListener(this);
        mSNewGame.setLayout(StringItem.LAYOUT_CENTER);
        
        mSSetting.setDefaultCommand(CMD_SELECT);
        mSSetting.setItemCommandListener(this);
        mSSetting.setLayout(StringItem.LAYOUT_CENTER);
        
        mSHighScore.setDefaultCommand(CMD_SELECT);
        mSHighScore.setItemCommandListener(this);
        mSHighScore.setLayout(StringItem.LAYOUT_CENTER);
        
        mSHelp.setDefaultCommand(CMD_SELECT);
        mSHelp.setItemCommandListener(this);
        mSHelp.setLayout(StringItem.LAYOUT_CENTER);
        
        mSAbout.setDefaultCommand(CMD_SELECT);
        mSAbout.setItemCommandListener(this);
        mSAbout.setLayout(StringItem.LAYOUT_CENTER);
        
        mMenu.append(mSNewGame);
        mMenu.append("\n");
        mMenu.append(mSSetting);
        mMenu.append("\n");
        mMenu.append(mSHighScore);
        mMenu.append("\n");
        mMenu.append(mSHelp);
        mMenu.append("\n");
        mMenu.append(mSAbout);
        
        mMenu.addCommand(CMD_EXIT);
        mMenu.addCommand(CMD_SELECT);
                
        mMenu.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mMenu);
    }
    
    public void createMenuNewGame()
    {
        cricketGame.addCommand(CMD_BACK);
        cricketGame.setCommandListener(this);
        Display.getDisplay(this).setCurrent(cricketGame);
        
    }
    
    public void createMenuSetting()
    {
        mSetting.addCommand(CMD_BACK);
        mSetting.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mSetting);
    }
    
    public void createMenuHighScore()
    {
        mHighScore.addCommand(CMD_BACK);
        mHighScore.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mHighScore);
    }
    
    
    public void createMenuHelp()
    {
        mHelp.addCommand(CMD_BACK);
        mHelp.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mHelp);
    }
    
    
    public void createMenuAbout()
    {
        mAbout.addCommand(CMD_BACK);
        mAbout.setCommandListener(this);
        Display.getDisplay(this).setCurrent(mAbout);
    }
    
    public void startApp() {
        createMainMenu();
    }
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c==CMD_BACK) {
            Display.getDisplay(this).setCurrent(mMenu);
        }
    }

    public void commandAction(Command c, Item item) {
        if (item.equals(mSNewGame)) {
            createMenuNewGame();
        }else{
            if(item.equals(mSSetting)){
                createMenuSetting();
            }else
            {
                if(item.equals(mSHighScore))
                {
                    createMenuHighScore();
                }else
                {
                    if (item.equals(mSHelp)) 
                    {
                        createMenuHelp();
                    }
                    else
                    {
                        createMenuAbout();
                    }
                }
            }
        }
        
    }
}
